<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" :isHaveAdd="false" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange" @sendUserSearch="receive_search" :searchData="searchDataInfo" :isHaveExport="false" @toAdd="openAdd">
      <template v-slot:searchBottom>
        <el-form class="search_prod search_prod1" label-width="94px">
          <el-form-item label="代理管理">
            <el-row class="tl mar_bottom_15">
              <el-radio-group v-model="isCheck" size="small" class="tl money-group" @change="checkChange">
                <el-radio-button v-for="(item, i) in checkOption" :label="item.iden" :key="i">{{ item.title }}</el-radio-button>
              </el-radio-group>
            </el-row>
          </el-form-item>
        </el-form>
      </template>
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" stripe v-loading="tableLoading" header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.8%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="头像" min-width="80">
            <template slot-scope="scope">
              <div class="img_bor_tx">
                <img :src="$fnc.getImgUrl(scope.row.avatar)" alt />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="用户信息" min-width="120">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>用户昵称：</span>
                  <span>{{ scope.row.nickname || "----" }}</span>
                </div>
                <div>
                  <span>会员账号：</span>
                  <span>{{ scope.row.username || "---" }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="身份证" min-width="120">
            <template slot-scope="scope">

              <div>
                <el-popover placement="right" title trigger="hover">
                  <img :src="scope.row.cardpositive" style="width: 500px" />
                  <img slot="reference" :src="scope.row.cardpositive" :alt="scope.row.cardpositive" style="width: 100px;height:70px;object-fit:cover" />
                </el-popover>
              </div>

              <div style="margin:5px 0">
                <el-popover placement="right" title trigger="hover">
                  <img :src="scope.row.cardnegative" style="width: 500px" />
                  <img slot="reference" :src="scope.row.cardnegative" :alt="scope.row.cardnegative" style="width: 100px;height:70px;object-fit:cover" />
                </el-popover>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="营业执照" min-width="120">
            <template slot-scope="scope">
              <div>
                <el-popover placement="right" title trigger="hover">
                  <img :src="scope.row.license" style="width: 500px" />
                  <img slot="reference" :src="scope.row.license" :alt="scope.row.license" style="width: 100px;height:70px;object-fit:cover" />
                </el-popover>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="申请信息" min-width="220">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>公司名称：</span>
                  <span>{{ scope.row.title || "----" }}</span>
                </div>
                <div>
                  <span>公司地址：</span>
                  <span>{{ scope.row.add || "---" }}</span>
                </div>
                <div>
                  <span>法人姓名：</span>
                  <span>{{ scope.row.name || "----" }}</span>
                </div>
                <div>
                  <span>身份证号：</span>
                  <span>{{ scope.row.card || "----" }}</span>
                </div>
                <div>
                  <span>备注：</span>
                  <span>{{ scope.row.remark || "----" }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="申请信息" min-width="180">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>代理级别：</span>
                  <el-tag v-if="scope.row.types == 3" size="mini" type="primary" effect="dark" style="color:#fff">省代</el-tag>
                  <el-tag v-else-if="scope.row.types == 2" size="mini" type="danger" effect="dark" style="color:#fff">市代</el-tag>
                  <el-tag v-else-if="scope.row.types == 1" size="mini" type="success" effect="dark" style="color:#fff">区代</el-tag>
                  <el-tag v-else-if="scope.row.types == 4" size="mini" type="warning" effect="dark" style="color:#fff">乡镇</el-tag>
                  <span v-else>---</span>
                </div>
                <div>
                  <span>代理区域：</span>
                  <span>{{ `${scope.row.province || ''}${scope.row.city || ''}${scope.row.area || ''}${scope.row.town || ''}`  || '---'}}</span>
                </div>
                <div>
                  <span>法人姓名：</span>
                  <span>{{ scope.row.name || "----" }}</span>
                </div>
                <div>
                  <span>身份证号：</span>
                  <span>{{ scope.row.card || "----" }}</span>
                </div>
                <div>
                  <span>申请时间：</span>
                  <span>{{ scope.row.created_time | getTimeFormat }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="审核状态" min-width="120">
            <template slot-scope="scope">
              <div class="user_table">
                <el-tag type="warning" v-if="scope.row.is_check == 0" effect="dark">审核中</el-tag>
                <el-tag type="success" v-else-if="scope.row.is_check == 1" effect="dark">审核通过</el-tag>
                <el-tag type="danger" v-else-if="scope.row.is_check == 2" effect="dark">已驳回</el-tag>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="220">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="openEdit(scope.row)" type="primary">审核</el-button>
                <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="toSupplierDetails(scope.row.id)">查看</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="moreDelete">删除</a>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" :title="isEdit ? '审核' : '添加'" class="tl size_13" width="800px" :visible.sync="dialogFormVisible">
      <el-form :model="edit_data" ref="form" label-width="120px">

        <el-form-item label="公司名称：" prop="title">
          <el-col :span="16">
            <el-input placeholder v-model="edit_data.title" clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="公司地址：" prop="add">
          <el-col :span="16">
            <el-input placeholder v-model="edit_data.add" clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="法人姓名：" prop="name">
          <el-col :span="16">
            <el-input placeholder v-model="edit_data.name" clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="身份证号：" prop="card">
          <el-col :span="16">
            <el-input placeholder v-model="edit_data.card" clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="代理级别：">
          <el-col :span="14">
            <el-radio-group v-model="edit_data.types">
              <el-radio label="3">省代</el-radio>
              <el-radio label="2">市代</el-radio>
              <el-radio label="1">区代</el-radio>
              <el-radio label="4">乡镇</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
        <el-form-item label="代理区域：">
          <el-col :span="14">
            <city-cascader size="large" :placeholder="`${edit_data.province || ''}${edit_data.city || ''}${edit_data.area || ''}${edit_data.town || ''}`" :level="edit_data.types == 3? 0:edit_data.types == 2? 1:edit_data.types == 1? 2: 3" @change="handleChange" />
          </el-col>
        </el-form-item>
        <el-form-item label="审核：">
          <el-col :span="14">
            <el-radio-group v-model="edit_data.is_check">
              <el-radio label="0">未审核</el-radio>
              <el-radio label="1">通过</el-radio>
              <el-radio label="2">驳回</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-col :span="16">
            <el-input placeholder v-model="edit_data.remark" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="saveEdit(edit_data)">保 存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- <keep-alive>
      <component :is="'xzuser'" :show="shows" :isAll="false" showType="list" @sendShop="getSend" @isshow="getShow"></component>
    </keep-alive> -->
  </div>
</template>
<script>
// import xzuser from "@/components/currency/xz_user.vue";
import uploadpic from "@/components/page/pic/up_pic";
import { checkUserAgentSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
import cityCascader from "@/components/currency/cityCascader/cityCascader";

export default {
  data() {
    return {
      isCheck: "",
      checkOption: [
        { iden: "", title: "全部" },
        { iden: 1, title: "审核已通过" },
        { iden: 0, title: "未审核" },
        { iden: 2, title: "审核不通过" },
      ],
      searchDataInfo: checkUserAgentSearchData,
      current: "",
      shows: false,
      isEdit: false,
      dialogFormVisible: false,
      edit_data: {
        piclink: "",
      },
      tableLoading: false,
      list: [],
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      multipleSelection: [],
    };
  },
  components: {
    uploadpic,
    cityCascader,
  },
  methods: {
    toSupplierDetails(id) {
      this.$store.commit("addbreadcrumbs", {
        title: "代理详情",
        url: `/index/user/checkagentdetails?id=${id}`,
      });
      this.$router.push("/index/user/checkagentdetails?id=" + id);
    },
    handleChange(value) {
      this.edit_data.province = "";
      this.edit_data.city = "";
      this.edit_data.area = "";
      this.edit_data.town = "";
      if (value) {
        for (var i = 0; i < value.length; i++) {
          if (i == 0) this.edit_data.province = value[i];
          if (i == 1) this.edit_data.city = value[i];
          if (i == 2) this.edit_data.area = value[i];
          if (i == 3) this.edit_data.town = value[i];
        }
      }
    },
    checkChange(val) {
      this.currentPage = 1;
      this.getNewsAll();
    },
    getShow(val) {
      this.shows = val;
    },
    getSend(info) {
      this.edit_data.username = info.username;
      this.edit_data.uid = info.id;
    },
    xzshop() {
      this.shows = true;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    moreDelete() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            this.$api.getAgent.delAgent({ id_str: str }).then((res) => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", res.data.result, "success");
                this.getNewsAll();
              } else {
                this.$swal("删除失败！", res.data.result, "error");
              }
            });
          }
        });
      }
    },
    openDelete(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getAgent.delAgent({ id_str: id }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", res.data.result, "success");
              this.getNewsAll();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },

    openAdd() {
      this.edit_data = {};
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    onSuccess(pic) {
      this.edit_data.piclink = pic;
    },
    openEdit(item) {
      this.isEdit = true;
      this.edit_data = JSON.parse(JSON.stringify(item));
      this.dialogFormVisible = true;
    },
    receive_search(obj) {
      this.currentPage = 1;
      this.searchAll = obj;
      this.getNewsAll();
    },
    handleSwitch(val, item) {
      var params = item;
      params.show = val;
      this.$api.getAgent.saveEditRemark(params).then((res) => {
        if (res.data.code != 200) {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getNewsAll();
        }
      });
    },

    sorta(a, b) {
      return b - a;
    },
    sortNew(index, item) {
      var params = item;
      params.sort = item.sort;
      this.$api.getAgent.saveEditRemark(params).then((res) => {
        if (res.data.code == 200) {
          return;
        } else {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getNewsAll();
        }
      });
    },
    saveEdit(item) {
      var params = {};
      params = item;
      if (this.isEdit == false) {
        this.$api.getAgent.addEditRemark(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.currentPage = 1;
            this.getNewsAll();
            this.$swal("添加成功！", res.data.result, "success");
          } else {
            this.$swal("添加失败！", res.data.result, "error");
          }
        });
      } else {
        this.$api.getAgent.checkAgent(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.getNewsAll();
            this.$swal("编辑成功！", res.data.result, "success");
          } else {
            this.$swal("编辑失败！", res.data.result, "error");
          }
        });
      }
    },
    getNewsAll() {
      this.tableLoading = true;
      var params = {};
      params = JSON.parse(JSON.stringify(this.searchAll));
      params.page = this.currentPage || 1;
      params.page_size = this.pageSize || "";
      for (var i in params) {
        if (!params[i]) {
          params[i] = "";
        }
      }
      var query = JSON.parse(JSON.stringify(this.queryObj || {}));
      var obj = Object.assign(query, params);
      obj.is_check = this.isCheck;
      this.$api.getAgent.getAgentApplyList(obj).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        setTimeout(() => {
          this.tableLoading = false;
        }, 200);
      });
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getNewsAll();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getNewsAll();
    },
  },
  created() {
    if (this.$route.query) {
      this.queryObj = this.$route.query;
      if (this.$route.query.page) {
        this.currentPage = this.$route.query.page;
        delete this.queryObj.page;
      }
      if (this.$route.query.is_check) {
        this.isCheck = this.$route.query.is_check;
        delete this.queryObj.is_check;
      }
    }

    this.getNewsAll();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
